package com.bstek.poc.plugin;

import android.app.Activity;
import android.content.*;
import android.os.Handler;
import android.os.Message;
import android.telephony.TelephonyManager;
import com.bstek.poc.WebActivity;
import com.bstek.poc.service.HeartbeatService;
import com.bstek.poc.utils.VPNUtils;
import jni.cylan.com.LoginReturn;
import jni.cylan.com.NativeFunction;
import jni.cylan.com.ProxyManager;
import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.splashscreen.SplashScreen;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;


/**
 * ┏┓　　　┏┓
 * ┏┛┻━━━┛┻┓
 * ┃　　　　　　　┃
 * ┃　　　━　　　┃
 * ┃　┳┛　┗┳　┃
 * ┃　　　　　　　┃
 * ┃　　　┻　　　┃
 * ┃　　　　　　　┃
 * ┗━┓　　　┏━┛
 * ┃　　　┃   神兽保佑
 * ┃　　　┃   代码无BUG！
 * ┃　　　┗━━━┓
 * ┃　　　　　　　┣┓
 * ┃　　　　　　　┏┛
 * ┗┓┓┏━┳┓┏┛
 * ┃┫┫　┃┫┫
 * ┗┻┛　┗┻┛
 * Created by leelucifer on 15/1/22.
 */
public class SetVPNValue extends CordovaPlugin {

    private boolean isOpenVpn=false;
    @Override
    public boolean execute(String action, JSONArray args, final CallbackContext callbackContext) throws JSONException {
        final Activity thisActivity=cordova.getActivity();
        if(action.equals("setVpn")) {
            JSONObject param=args.getJSONObject(0);
            final String ip = param.getString("ip");
            final String passport = param.getString("passport");
            final String password = param.getString("password");

            cordova.getThreadPool().execute(new Runnable() {
                @Override
                public void run() {
                    isOpenVpn = setVpn(ip, passport, password);
                    if (isOpenVpn) {
//                        callbackContext.success(1);
                        SharedPreferences sp=cordova.getActivity().getSharedPreferences("VPN_POC",Context.MODE_PRIVATE);
                        sp.edit().putString("user",passport).commit();
                        Intent intent=new Intent(thisActivity,WebActivity.class);
                        thisActivity.startActivity(intent);
                        callbackContext.success(1);
                    }else{
                        callbackContext.error(0);
                    }
                }
            });


            return true;
        }
        if(action.equals("closeVpn")){
            boolean closeFlag=destroyVPN();
            if(closeFlag)
                callbackContext.success();
            else
                callbackContext.error(0);
        }
        if(action.equals("changePwd")){
            JSONObject param=args.getJSONObject(0);
            String oldPwd = param.getString("oldPwd");
            String newPwd = param.getString("newPwd");
            boolean closeFlag=changePwd(oldPwd,newPwd);
            if(closeFlag)
                callbackContext.success();
            else
                callbackContext.error(0);
        }
        return false;
    }

    private boolean setVpn(String ip,String passport,String password){
        boolean openVpn= VPNUtils.openVPN(cordova.getActivity(),ip,passport,password,getDeviceId());
        return openVpn;
    }

    private String getDeviceId(){
        return ((TelephonyManager)cordova.getActivity().getSystemService(Context.TELEPHONY_SERVICE)).getDeviceId();
    }
    /**
     * 关闭成功,会返回true,反之则返回false
     * @return
     */
    private boolean destroyVPN() {
        return VPNUtils.closeVPN(cordova.getActivity());
    }
    /**
     * 修改密码成功,会返回true,反之则返回false
     * @return
     */
    private boolean changePwd(String oldPwd,String newPwd){
        int ret=0;
        ret = jni.cylan.com.cylanlib.changePwd(oldPwd,newPwd);
        return ret!=0;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}
